package com.example.li_han.servlet;

import com.example.li_han.service.AdminService;
import com.example.li_han.pojo.Admin;
import com.google.gson.Gson;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "AdminLoginServlet", value = "/AdminLoginServlet")
public class AdminLoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("UTF-8");

        System.out.println("=== 管理员登录Servlet被调用 ===");

        try {
            // 读取请求数据
            BufferedReader br = req.getReader();
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

            String requestBody = sb.toString();
            System.out.println("管理员登录请求体: " + requestBody);

            // 使用Gson解析JSON
            Gson gson = new Gson();
            Map requestMap = gson.fromJson(requestBody, Map.class);

            String zhangHao = (String) requestMap.get("zhangHao");
            String password = (String) requestMap.get("password");

            System.out.println("解析出的管理员账号: " + zhangHao + ", 密码: " + password);

            Map<String, Object> result = new HashMap<>();

            if (zhangHao == null || password == null || zhangHao.trim().isEmpty() || password.trim().isEmpty()) {
                result.put("code", 500);
                result.put("msg", "账号和密码不能为空");
                resp.getWriter().write(gson.toJson(result));
                return;
            }

            // 调用AdminService进行数据库验证
            AdminService adminService = new AdminService();
            Admin admin = adminService.login_check(zhangHao, password);

            if (admin != null) {
                Map<String, Object> adminData = new HashMap<>();
                adminData.put("zhangHao", admin.getZhangHao());
                adminData.put("userName", "系统管理员");
                adminData.put("adminId", admin.getAdminId());
                adminData.put("avatar", admin.getAvatar());
                adminData.put("role", "admin");

                result.put("code", 200);
                result.put("msg", "管理员登录成功");
                result.put("data", adminData);
                System.out.println("管理员登录: 数据库验证成功");
            } else {
                result.put("code", 500);
                result.put("msg", "管理员账号或密码错误");
                System.out.println("管理员登录: 数据库验证失败");
            }

            String json = gson.toJson(result);
            System.out.println("管理员登录响应: " + json);
            resp.getWriter().write(json);

        } catch (Exception e) {
            System.err.println("管理员登录异常:");
            e.printStackTrace();
            Map<String, Object> error = new HashMap<>();
            error.put("code", 500);
            error.put("msg", "管理员登录异常: " + e.getMessage());
            resp.getWriter().write(new Gson().toJson(error));
        }
    }
}